package com.jinmdz.fmis.generator.license;

import com.jinmdz.fmis.core.license.CertificateKeyStoreParam;
import com.jinmdz.fmis.core.license.CertificateLicenseManager;
import com.jinmdz.fmis.core.util.LogUtil;
import de.schlichtherle.license.*;

import javax.security.auth.x500.X500Principal;
import java.io.File;
import java.util.prefs.Preferences;

/**
 * License生成类
 * (源自网络)
 *
 * @author LiCongLu
 * @date 2020-01-08 15:18
 */
public class LicenseCreator {

    private final static X500Principal DEFAULT_HOLDER_AND_ISSUER = new X500Principal("CN=www.jinmdz.com, OU=www.jinmdz.com, O=www.jinmdz.com, L=SH, ST=SH, C=CN");

    private LicenseModel licenseModel;

    public LicenseCreator(LicenseModel licenseModel) {
        this.licenseModel = licenseModel;
    }

    /**
     * 生成License证书
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2020-01-08 15:33
     */
    public boolean generateLicense() {
        try {
            LicenseManager licenseManager = new CertificateLicenseManager(initLicenseParam(), null);
            LicenseContent licenseContent = initLicenseContent();
            licenseManager.store(licenseContent, new File(licenseModel.getLicensePath()));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.error("证书生成失败：" + licenseModel.toString(), e);
            return false;
        }
    }

    /**
     * 初始化证书生成参数
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2020-01-08 15:33
     */
    private LicenseParam initLicenseParam() {
        Preferences preferences = Preferences.userNodeForPackage(LicenseCreator.class);

        //设置对证书内容加密的秘钥
        CipherParam cipherParam = new DefaultCipherParam(licenseModel.getStorePass());

        KeyStoreParam privateStoreParam = new CertificateKeyStoreParam(LicenseCreator.class
                , licenseModel.getPrivateKeyStorePath()
                , licenseModel.getPrivateAlias()
                , licenseModel.getStorePass()
                , licenseModel.getKeyPass());

        return new DefaultLicenseParam(licenseModel.getSubject()
                , preferences
                , privateStoreParam
                , cipherParam);
    }

    /**
     * 设置证书生成正文信息
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2020-01-08 15:34
     */
    private LicenseContent initLicenseContent() {
        LicenseContent licenseContent = new LicenseContent();
        licenseContent.setHolder(DEFAULT_HOLDER_AND_ISSUER);
        licenseContent.setIssuer(DEFAULT_HOLDER_AND_ISSUER);

        licenseContent.setSubject(licenseModel.getSubject());
        licenseContent.setIssued(licenseModel.getIssuedTime());
        licenseContent.setNotBefore(licenseModel.getIssuedTime());
        licenseContent.setNotAfter(licenseModel.getExpiryTime());
        licenseContent.setConsumerType(licenseModel.getConsumerType());
        licenseContent.setConsumerAmount(licenseModel.getConsumerAmount());
        licenseContent.setInfo(licenseModel.getDescription());

        //扩展校验，这里可以自定义一些额外的校验信息(也可以用json字符串保存)
        if (licenseModel.getExtraModel() != null) {
            licenseContent.setExtra(licenseModel.getExtraModel());
        }
        return licenseContent;
    }
}
